package com.qfedu.spring.springmvc.controller;

import com.qfedu.spring.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/4/29 17:34
 */
@Controller
@RequestMapping("/data")
@SessionAttributes({"name","age"})
// model中的 name和age 会存入session中
public class DataController {

    // 原始的传值方式
    @RequestMapping("/test1")
    public String test1(HttpServletRequest request, HttpSession session){
        // 到request 域中存数据
        request.setAttribute("name","小高");
        request.setAttribute("age",13);
        // session 存数据
        session.setAttribute("date","2022");
        return "wecome";
    }

    // Spring 推荐的传值方式
    @RequestMapping("/test2")
    public String test2(Model model){
        //model 中的数据是放到request 中的
        model.addAttribute("name","超哥");
        model.addAttribute("age",44);
        // 如果没有指定key的话，Key 默认几句话四value 值的类型，这个是string
        model.addAttribute("小庄");
        User user = new User();
        user.setId(11);
        user.setName("西铭");
        model.addAttribute(user);
        return "wecome";
    }

    @RequestMapping("/test3")
    public ModelAndView test3(){
        ModelAndView modelAndView = new ModelAndView("wecome");
        modelAndView.addObject("name","肖光明");
        return modelAndView;
    }
}
